﻿Imports DTO
Imports System.Data.SqlClient
Public Class CChiTietPhieuMuonDAO
    Public Function ThemChiTietPhieuMuon(ByRef _chitiet As CChiTietPhieuMuonDTO) As Integer

        Using cn As SqlConnection = CDataProvider.Connect()
            Dim cm As New SqlCommand("uspThemChiTietPhieuMuon", cn)
            cm.CommandType = CommandType.StoredProcedure
            Dim para As SqlParameter = New SqlParameter("@ngaytra", _chitiet.NgayTra)
            cm.Parameters.Add(para)
            para = New SqlParameter("@maphieumuon", _chitiet.MaPhieuMuon)
            cm.Parameters.Add(para)
            para = New SqlParameter("@masach", _chitiet.MaSach)
            cm.Parameters.Add(para)
            para = New SqlParameter("@ma", _chitiet.Ma)
            para.Direction = ParameterDirection.Output
            cm.Parameters.Add(para)
            Dim n As Integer = cm.ExecuteNonQuery()
            cn.Close()
            _chitiet.Ma = cm.Parameters("@ma").Value
            Return n
        End Using
    End Function

    Public Function LayChiTietPhieuMuonTheoMa(ByVal _ma As Integer) As List(Of CChiTietPhieuMuonDTO)

        Using cn As SqlConnection = CDataProvider.Connect()
            Dim cm As New SqlCommand("uspLayChiTietPhieuMuonTheoMa", cn)
            cm.CommandType = CommandType.StoredProcedure
            Dim para As SqlParameter = New SqlParameter("@ma", _ma)
            cm.Parameters.Add(para)
            Dim lChiTietPhieu As New List(Of CChiTietPhieuMuonDTO)

            Using reader As IDataReader = cm.ExecuteReader(CommandBehavior.CloseConnection)
                While reader.Read()
                    lChiTietPhieu.Add(New CChiTietPhieuMuonDTO(reader))
                End While
            End Using
            Return lChiTietPhieu
        End Using
    End Function

    Public Function ThongKeSachTraTre() As List(Of CChiTietPhieuMuonDTO)
        Using cn As SqlConnection = CDataProvider.Connect()
            Dim cm As New SqlCommand("uspThongKeSachTraTre", cn)
            cm.CommandType = CommandType.StoredProcedure
            Dim lChiTietPhieu As New List(Of CChiTietPhieuMuonDTO)

            Using reader As IDataReader = cm.ExecuteReader(CommandBehavior.CloseConnection)
                While reader.Read()
                    lChiTietPhieu.Add(New CChiTietPhieuMuonDTO(reader("MaSach"), reader("NgayMuon"), reader("SoNgayTraTre")))
                End While
            End Using
            Return lChiTietPhieu
        End Using
    End Function
End Class
